package com.hongxun.project.assets.pPoint.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.common.utils.text.Convert;
import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.service.IILineService;
import com.hongxun.project.assets.pBoxes.domain.PBoxes;
import com.hongxun.project.assets.pBoxes.service.IPBoxesService;
import com.hongxun.project.assets.pCablePit.domain.PCablePit;
import com.hongxun.project.assets.pCablePit.service.IPCablePitService;
import com.hongxun.project.assets.pPole.domain.PPole;
import com.hongxun.project.assets.pPole.service.IPPoleService;
//import com.sun.org.apache.xpath.internal.operations.Mod;
//import com.sun.xml.internal.bind.v2.TODO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 点位采集基础 信息操作处理
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/pPoint")
public class PPointController extends BaseController
{
    private String prefix = "assets/pPoint";
	
	@Autowired
	private IPPointService pPointService;
	@Autowired
	private IILineService iLineService;
	@Autowired
	private IPPoleService pPoleService;
	@Autowired
	private IBByqService bByqService;
	@Autowired
	private IPBoxesService pBoxesService;
	@Autowired
	private IPCablePitService pCablePitService;



	
	@RequiresPermissions("assets:pPoint:view")
	@GetMapping()
	public String pPoint()
	{
	    return prefix + "/pPoint";
	}


	/**
	 * 同杆点位选择
	 * @return
	 */
	@GetMapping("/choosePole")
	public String choosePole(PPoint pPoint , ModelMap modelMap)
	{
		String poleId= pPoint.getId();
		String taskId= pPoint.getTaskId();
		String byqId = pPoint.getByqId();
		modelMap.put("Ids",poleId);
		modelMap.put("byqIds",byqId);
		modelMap.put("poleIds",poleId);
		modelMap.put("taskIds",taskId);
		return prefix + "/choosePole";
	}


	/**
	 * 查询点位采集基础列表
	 */
	@RequiresPermissions("assets:pPoint:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PPoint pPoint)
	{
		startPage();
        List<PPoint> list = pPointService.selectPPointList(pPoint);
		return getDataTable(list);
	}

	@PostMapping("/listIsPole")
	@ResponseBody
	public TableDataInfo listIsPole(PPoint pPoint)
	{
		startPage();
		String byqId = pPoint.getByqId();
		String id = pPoint.getId();
		String poleId = pPoint.getPoleId();
		pPoint.setDatastate("删除");
		List<PPoint> listss = pPointService.selectlistIsPole(pPoint);
		List<PPoint> list = new ArrayList<>();
		for (PPoint point : listss) {
			ILine iLine = iLineService.selectILineById(point.getLineId());
			point.setLineName(iLine.getName());
			list.add(point);
		}

		//	分两种情况，①同杆点位有共同的上联点位 ②同杆点位分属关联不同的上联点位
		if(list.size() == 1){
			/*PPoint pPoint1 = pPointService.selectPPointByIdForState(id,pPoint.getTaskId());
			String upId = pPoint1.getUpId();
			PPoint pPoint2 = new PPoint();
			pPoint2.setUpId(upId);
			pPoint2.setDatastate("删除");
			List<PPoint> pPoints = pPointService.selectPPointList(pPoint2);

			return getDataTable(pPoints);*/


				//	①
				PPoint pPoint1 = pPointService.selectPPointById(id,pPoint.getTaskId());
				String upId = pPoint1.getUpId();
				PPoint pPoint2 = new PPoint();
				pPoint2.setUpId(upId);
				pPoint2.setTaskId(pPoint.getTaskId());
				List<PPoint> pPoints = pPointService.selectPPointList(pPoint2);

				//	②
				if(pPoints.size() == 1){
					/*PPoint pPoint3 = new PPoint();
					pPoint3.setPoleId(list.get(0).getPoleId());*/

					pPoint.setId(pPoints.get(0).getPoleId());
					pPoint.setPoleId(pPoints.get(0).getPoleId());

					List<PPoint> pPoints1 = pPointService.selectlistIsPole(pPoint);
					for (PPoint point : pPoints1) {
						ILine iLine1 = iLineService.selectILineById(point.getLineId());
						point.setLineName(iLine1.getName());
					}
					return getDataTable(pPoints1);
				}else if(pPoints.size() > 1){
					for (PPoint point : pPoints) {
						ILine iLine1 = iLineService.selectILineById(point.getLineId());
						point.setLineName(iLine1.getName());
					}
					return getDataTable(pPoints);
				}
				return getDataTable(pPoints);


		}else if (list.size() > 1){
			return getDataTable(list);
		}

		//	通过id查询关联的上联id，通过上联id来查询有多少同杆点位

		return getDataTable(list);

	}
	
	
	/**
	 * 导出点位采集基础列表
	 */
	@RequiresPermissions("assets:pPoint:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PPoint pPoint)
    {
    	List<PPoint> list = pPointService.selectPPointList(pPoint);
        ExcelUtil<PPoint> util = new ExcelUtil<PPoint>(PPoint.class);
        return util.exportExcel(list, "pPoint");
    }
	
	/**
	 * 新增点位采集基础
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存点位采集基础
	 */
	@RequiresPermissions("assets:pPoint:add")
	@Log(title = "点位采集基础", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PPoint pPoint)
	{
		pPoint.setDatastate("新增");
		return toAjax(pPointService.insertPPoint(pPoint));
	}

	/**
	 * 点位与杆塔合并
	 */
	@GetMapping("/edit/{id}/{taskId}")
	public String edit(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
//		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getUpId(),taskId);
		PPoint pPoint1 = new PPoint();
		pPoint1.setId(pPoint.getUpId());
		pPoint1.setDatastate("删除");
		List<PPoint> pPointList = pPointService.selectPPointList(pPoint1);
		ILine iLine = iLineService.selectILineById(pPoint.getLineId());
//		if(pPoint1 != null)
//			pPoint.setUpId(pPoint1.getCode());

		if(pPointList.size() != 0){
			for (PPoint point : pPointList) {
				pPoint.setUpId(point.getCode());
			}
		}

		if(iLine != null)
			pPoint.setLineName(iLine.getName());
		else
			pPoint.setLineName("");




		PPole pPole1 = new PPole();
		pPole1.setPId(id);
		pPole1.setDatastate("删除");
		List<PPole> pPoleList = pPoleService.selectPPoleList(pPole1);
		PPole pPole = new PPole();
		if(pPoleList.size() == 0){
//			PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
			pPole1.setPId( pPoint.getPoleId());
			pPoleList = pPoleService.selectPPoleList(pPole1);
			pPole = pPoleService.selectPPoleByIdForState(pPoleList.get(0).getId(),taskId);
		}else{
			pPole = pPoleService.selectPPoleByIdForState(pPoleList.get(0).getId(),taskId);
		}
		mmap.put("pPole", pPole);



		mmap.put("pPoint", pPoint);
	    return prefix + "/edit";
	}

	/**
	 * 点位与变压器合并
	 */
	@GetMapping("/editForPointByq/{id}/{taskId}")
	public String editForPointByq(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
//		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getUpId(),taskId);
		PPoint pPoint1 = new PPoint();
		pPoint1.setId(pPoint.getUpId());
		pPoint1.setDatastate("删除");
		List<PPoint> pPointList = pPointService.selectPPointList(pPoint1);
		ILine iLine = iLineService.selectILineById(pPoint.getLineId());
//		if(pPoint1 != null)
//			pPoint.setUpId(pPoint1.getCode());

		if(pPointList.size() != 0){
			for (PPoint point : pPointList) {
				pPoint.setUpId(point.getCode());
			}
		}

		if(iLine != null)
			pPoint.setLineName(iLine.getName());
		else
			pPoint.setLineName("");


		BByq bByq = new BByq();
		bByq.setPointId(id);
		List<BByq> bByqList = bByqService.selectBByqList(bByq);
		BByq byq = new BByq();
		if(bByqList.size() == 0){
			bByq.setPointId(pPoint.getPoleId());
			bByqList = bByqService.selectBByqList(bByq);
			byq = bByqService.selectBByqById(bByqList.get(0).getId());
		}else{
			byq = bByqService.selectBByqById(bByqList.get(0).getId());
		}
		mmap.put("bByq", byq);



		mmap.put("pPoint", pPoint);
	    return prefix + "/editForPointByq";
	}

	/**
	 * 点位与分支箱合并
	 */
	@GetMapping("/editForPointFzx/{id}/{taskId}")
	public String editForPointFzx(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
//		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getUpId(),taskId);
		PPoint pPoint1 = new PPoint();
		pPoint1.setId(pPoint.getUpId());
		pPoint1.setDatastate("删除");
		List<PPoint> pPointList = pPointService.selectPPointList(pPoint1);
		ILine iLine = iLineService.selectILineById(pPoint.getLineId());
//		if(pPoint1 != null)
//			pPoint.setUpId(pPoint1.getCode());

		if(pPointList.size() != 0){
			for (PPoint point : pPointList) {
				pPoint.setUpId(point.getCode());
			}
		}

		if(iLine != null)
			pPoint.setLineName(iLine.getName());
		else
			pPoint.setLineName("");

		mmap.put("pPoint", pPoint);
	    return prefix + "/editForPointFzx";
	}

	/**
	 * 点位与下户点合并
	 */
	@GetMapping("/editForPointXhd/{id}/{taskId}")
	public String editForPointXhd(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
//		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getUpId(),taskId);
		PPoint pPoint1 = new PPoint();
		pPoint1.setId(pPoint.getUpId());
		pPoint1.setDatastate("删除");
		List<PPoint> pPointList = pPointService.selectPPointList(pPoint1);
		ILine iLine = iLineService.selectILineById(pPoint.getLineId());
//		if(pPoint1 != null)
//			pPoint.setUpId(pPoint1.getCode());

		if(pPointList.size() != 0){
			for (PPoint point : pPointList) {
				pPoint.setUpId(point.getCode());
			}
		}

		if(iLine != null)
			pPoint.setLineName(iLine.getName());
		else
			pPoint.setLineName("");

		mmap.put("pPoint", pPoint);
	    return prefix + "/editForPointXhd";
	}

	/**
	 * 点位与电缆井合并
	 */
	@GetMapping("/editForPointDlj/{id}/{taskId}")
	public String editForPointDlj(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
//		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getUpId(),taskId);
		PPoint pPoint1 = new PPoint();
		pPoint1.setId(pPoint.getUpId());
		pPoint1.setDatastate("删除");
		List<PPoint> pPointList = pPointService.selectPPointList(pPoint1);
		ILine iLine = iLineService.selectILineById(pPoint.getLineId());
//		if(pPoint1 != null)
//			pPoint.setUpId(pPoint1.getCode());

		if(pPointList.size() != 0){
			for (PPoint point : pPointList) {
				pPoint.setUpId(point.getCode());
			}
		}

		if(iLine != null)
			pPoint.setLineName(iLine.getName());
		else
			pPoint.setLineName("");


		PCablePit pCablePit = new PCablePit();
		pCablePit.setPId(id);
		pCablePit.setDatastate("删除");
		List<PCablePit> pCablePits = pCablePitService.selectPCablePitList(pCablePit);
		PCablePit pCablePit1 = new PCablePit();
		if(pCablePits.size() == 0){
			pCablePit.setPId(pPoint.getPoleId());
			pCablePits = pCablePitService.selectPCablePitList(pCablePit);
			pCablePit1 = pCablePitService.selectPCablePitByIdForState(pCablePits.get(0).getId(),taskId);
		}else{
			pCablePit1 = pCablePitService.selectPCablePitByIdForState(pCablePits.get(0).getId(),taskId);
		}
		mmap.put("pCablePit", pCablePit1);



		mmap.put("pPoint", pPoint);
	    return prefix + "/editForPointDlj";
	}

	/**
	 * 点位与表箱合并
	 */
	@GetMapping("/editForPointBx/{id}/{taskId}")
	public String editForPointBx(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
//		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getUpId(),taskId);
		PPoint pPoint1 = new PPoint();
		pPoint1.setId(pPoint.getUpId());
		pPoint1.setDatastate("删除");
		List<PPoint> pPointList = pPointService.selectPPointList(pPoint1);
		ILine iLine = iLineService.selectILineById(pPoint.getLineId());
//		if(pPoint1 != null)
//			pPoint.setUpId(pPoint1.getCode());

		if(pPointList.size() != 0){
			for (PPoint point : pPointList) {
				pPoint.setUpId(point.getCode());
			}
		}

		if(iLine != null)
			pPoint.setLineName(iLine.getName());
		else
			pPoint.setLineName("");


		PBoxes pBoxes = new PBoxes();
		pBoxes.setPId(id);
		pBoxes.setDatastate("删除");
		List<PBoxes> pBoxesList = pBoxesService.selectPBoxesList(pBoxes);
		PBoxes pBoxes1 = new PBoxes();
		if(pBoxesList.size() == 0){
			pBoxes.setPId(pPoint.getPoleId());
			pBoxesList = pBoxesService.selectPBoxesList(pBoxes);
			pBoxes1 = pBoxesService.selectPBoxesByIdForState(pBoxesList.get(0).getId(),taskId);
		}else {
			pBoxes1 = pBoxesService.selectPBoxesByIdForState(pBoxesList.get(0).getId(),taskId);
		}


		mmap.put("pBoxes", pBoxes1);



		mmap.put("pPoint", pPoint);
	    return prefix + "/editForPointBx";
	}

	/*@PostMapping("/editPoint")
	@ResponseBody
	public String editPoint(@RequestParam Map<String , Object> objectMap, ModelMap mmap)
	{
		String id = objectMap.get("id").toString();
		String taskId = objectMap.get("taskId").toString();
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
		mmap.put("pPoint", pPoint);
		return prefix + "/edit";
	}*/
	
	/**
	 * 修改保存点位采集基础
	 */
	@RequiresPermissions("assets:pPoint:edit")
	@Log(title = "点位采集基础", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PPoint pPoint)
	{
		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getId(),pPoint.getTaskId());
		pPoint.setUpId(pPoint1.getUpId());
		pPoint.setDatastate("修改");
		return toAjax(pPointService.updatePPoint(pPoint));
	}
	
	/**
	 * 删除点位采集基础
	 */
	@RequiresPermissions("assets:pPoint:remove")
	@Log(title = "点位采集基础", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(pPointService.deletePPointByIds(ids));
	}



	/**
	 * 删除点位采集基础
	 */
	@RequiresPermissions("assets:pPoint:remove")
	@Log(title = "点位采集基础", businessType = BusinessType.DELETE)
	@PostMapping( "/remove2Id")
	@ResponseBody
	public AjaxResult remove2Id(String ids , String taskIds)
	{
		String[] str = Convert.toStrArray(ids);
		int i = 0;
		for (String s : str) {
			PPoint pPoint = new PPoint();
			pPoint.setId(s);
			pPoint.setTaskId(taskIds);
			pPoint.setDatastate("删除");
			i = pPointService.updatePPoint(pPoint);
		}
		return toAjax(i);
//		return toAjax(pPointService.deletePPointByIdsAndTaskIds(ids , taskIds));
	}


	/**
	 * 交叉跨越点位采集基础
	 */
	@GetMapping("/crossoverEdit/{id}/{taskId}")
	public String crossoverEdit(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
//		mmap.put("line_id", pPoint.getLineId());
//		mmap.put("up_id", pPoint.getUpId());
//		mmap.put("lower_id", pPoint.getLowerId());
		ILine iLine = iLineService.selectILineById(pPoint.getLineId());
		pPoint.setLineId(iLine.getName());	//	线路名称

//			上联点位编号赋值
		PPoint pPoint1 = new PPoint();
		pPoint1.setId(pPoint.getUpId());
		pPoint1.setDatastate("删除");
		List<PPoint> pPointList = pPointService.selectPPointList(pPoint1);
		for (PPoint point : pPointList) {
			pPoint.setUpId(point.getCode());
		}
		//	下联点位编号赋值
		pPoint1.setId(pPoint.getLowerId());
		List<PPoint> pPointList1 = pPointService.selectPPointList(pPoint1);
		for (PPoint point : pPointList1) {
			pPoint.setLowerId(point.getCode());
		}

		mmap.put("pPoint", pPoint);
		return prefix + "/crossoverEdit";
	}


	/**
	 * 	交叉跨越点保存信息
	 * @param pPoint
	 * @return
	 */
	@PostMapping("/crossoverEdit")
	@ResponseBody
	public AjaxResult crossoverEdit(PPoint pPoint , @RequestParam Map<String , Object> objectMap)
	{
		PPoint pPoint1 = pPointService.selectPPointByIdForState(pPoint.getId(),pPoint.getTaskId());
		pPoint.setLineId(pPoint1.getLineId());
		pPoint.setUpId(pPoint1.getUpId());
		pPoint.setLowerId(pPoint1.getLowerId());
		return toAjax(pPointService.updatePPoint(pPoint));
	}




}
